<?php

namespace oa\modules\contract\controllers;

use Yii;
use oa\services\CustomerService;
use oa\services\ContractService;

class DefaultController extends BaseController
{
	public function actionIndex()
	{
        $this->setViewBreadcrumbs(['合同', '合同列表']);
        if ($this->request->isAjax) {
            return ContractService::getList();
        }
        $end_time = time();
        $start_time = $end_time - 90 * 86400;
        $timeRange = date('Y-m-d', $start_time) . " ~ " . date('Y-m-d', $end_time);
        return $this->fetch(['timeRange' => $timeRange]);
	}

	public function actionDeliveries()
	{
		$id = $this->input('id');
		$data = ContractService::getDeliveries($id);
		return $this->fetch(['data' => $data], 2);
	}

	public function actionAdd()
	{
		if ($this->request->isPost) {
			$res = ContractService::save($this->request->post());
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err('提交失败', $res);
        }

        $this->setViewBreadcrumbs(['合同', '合同列表', '录入合同']);
        $pid = $this->input('pid');
		return $this->fetch(['pid' => $pid]);
	}

	public function actionEdit()
	{
		$id = $this->input('id');
		if ($this->request->isPost) {
			$res = ContractService::save($id);
			if ($res === true) {
				return $this->ok('提交成功');
			}
			return $this->err($res);
        }
		$data = ContractService::one($id, 1);
		return $this->fetch(['data' => $data], 2);
	}

	public function actionRemove()
	{
		if ($this->request->isAjax) {
			if (ContractService::remove($this->input('id'))) {
				return $this->ok('删除成功');
			}
			return $this->err('删除失败');
        }
	}

	public function actionComplete()
	{
		if ($this->request->isAjax) {
			if (ContractService::complete($this->input('id'))) {
				return $this->ok('操作成功');
			}
			return $this->err('操作失败');
        }
	}

	public function actionAudit()
	{
		if ($this->request->isAjax) {
            return ContractService::getAuditList();
        }
        return $this->fetch();
	}

	public function actionReject()
	{
		if ($this->request->isAjax) {
			if (ContractService::reject($this->input('id'), $this->input('audit_msg'))) {
				return $this->ok('审核成功');
			}
			return $this->err('审核失败');
        }
	}

	public function actionApprove()
	{
		if ($this->request->isAjax) {
			if (ContractService::approve($this->input('id'))) {
				return $this->ok('审核成功');
			}
			return $this->err('审核失败');
        }
	}

    public function actionCustomers()
    {
        if ($this->request->isAjax) {
            return ContractService::getCustomerList();
        }
    }
	
	public function actionProductPlan()
	{
		
	}
}
